package cn.newtouch.pet.controller;

import cn.newtouch.basic.exception.BusinessException;
import cn.newtouch.basic.util.JsonResult;
import cn.newtouch.basic.util.LoginContext;
import cn.newtouch.basic.util.PageList;
import cn.newtouch.pet.domain.SearchMasterMsg;
import cn.newtouch.pet.dto.AcceptDto;
import cn.newtouch.pet.query.SearchMasterMsgQuery;
import cn.newtouch.pet.service.ISearchMasterMsgService;
import cn.newtouch.user.domain.Logininfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return JsonResult 响应给前端
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return JsonResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return JsonResult 响应给前端
    */
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
                searchMasterMsgService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id")Long id) {

        return searchMasterMsgService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> findAll(){
        return searchMasterMsgService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }


    /**
     * 寻主
     * @return
     */
    @PostMapping("/publish")
    @ApiOperation(value = "寻主" )
    public JsonResult publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request){
        try {
            //获取logininfo
            Logininfo logininfo = LoginContext.getLogininfo(request);
            searchMasterMsgService.publish(smm,logininfo.getId());
            return JsonResult.me();
        }  catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常！！");
        }
    }

    /**
     * 待处理
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/toHandle")
    @ApiOperation(value = "待处理接口" )
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {

        Logininfo logininfo = LoginContext.getLogininfo(request);

        return searchMasterMsgService.toHandle(query,logininfo.getId());
    }

    /**
     * 待审核
     * @param query 查询对象
     * @return PageList 分页对象
     */
   @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);

        return searchMasterMsgService.toAudit(query,logininfo.getId());
    }

    /**
     * 用户信息
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {

        Logininfo logininfo = LoginContext.getLogininfo(request);

        return searchMasterMsgService.user(query,logininfo.getId());
    }

    /**
     * 接单
     * @param
     * @return JsonResult 响应给前端
     */
    @PostMapping("/accept")
    public JsonResult accept(@RequestBody AcceptDto dto, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            searchMasterMsgService.accept(dto,logininfo.getId());
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 拒单
     * @param
     * @return JsonResult 响应给前端
     */
    @GetMapping("/reject/{id}")
    public JsonResult reject(@PathVariable("id") Long msgId){
        try {
            searchMasterMsgService.reject(msgId);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }
}
